package com.feishi.core.media.opencv;

import com.feishi.core.media.opencv.util.OpenCvUtil;
import com.feishi.core.util.common.FileUtil;
import com.feishi.core.util.common.bean.WaveData;
import com.feishi.core.util.compress.CompressUtil;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;

import static org.opencv.core.Core.absdiff;
import static org.opencv.imgproc.Imgproc.*;

/**
 * @version 1.0
 * @auth caixq
 * @time 18-4-24 13:17
 **/
public class FindMove {
    Mat background;
    Mat preMat;
    Mat currentGrayMat;
    double diff;
    private String outPath;
    private int index;
    private WaveData waveData=new WaveData(100);
    private Mat currentMat;
    private Mat diff_thresh;

    public void onMatReceive(Mat currentMat){
        this.currentMat=currentMat;
        this.currentGrayMat =new Mat();

        //1.将currentMat转为灰度图
        cvtColor(currentMat, this.currentGrayMat, COLOR_BGR2GRAY);

        preMat=preMat==null?currentMat:preMat;

        boolean b = moveDetect(preMat, currentMat);
        if(moveDetect(preMat, currentMat)){
            Imgcodecs.imwrite(outPath+"/out"+index+".png",this.currentMat);
            Imgcodecs.imwrite(outPath+"/A/out"+index+".png",diff_thresh);
        }

        preMat= currentMat;
    }

    private boolean moveDetect(Mat preMat, Mat currentMat) {
        index++;

        //2.将background和frame做差
        Mat diff=new Mat();
        absdiff(currentMat, preMat, diff);
        //3.对差值图diff_thresh进行阈值化处理
        this.diff_thresh=new Mat();
        threshold(diff, diff_thresh, 50, 255, THRESH_BINARY);

        //4.腐蚀
        Mat kernel_erode = getStructuringElement(MORPH_RECT, new Size(3, 3));
        Mat kernel_dilate = getStructuringElement(MORPH_RECT, new Size(18, 18));
        erode(diff_thresh, diff_thresh, kernel_erode);

//        OpenCvUtil.MatToBytes(diff_thresh);
        int size=CompressUtil.gZip(OpenCvUtil.MatToBytes(diff_thresh,".jpg")).length;
        boolean result=waveData.isWaveData((double) size);
        waveData.addData((double) size);

        return result;//返回result
    }

//    private Mat moveDetect(Mat temp, Mat frame) {
//        index++;
//        //1.将background和frame转为灰度图
//        Mat gray1=new Mat(), gray2=new Mat();
//        cvtColor(temp, gray1, COLOR_BGR2GRAY);
//        cvtColor(frame, gray2, COLOR_BGR2GRAY);
//        //2.将background和frame做差
//        Mat diff=new Mat();
//        absdiff(gray1, gray2, diff);
//        //3.对差值图diff_thresh进行阈值化处理
//        Mat diff_thresh=new Mat();
//        threshold(diff, diff_thresh, 50, 255, THRESH_BINARY);
//
//        //4.腐蚀
//        Mat kernel_erode = getStructuringElement(MORPH_RECT, new Size(3, 3));
//        Mat kernel_dilate = getStructuringElement(MORPH_RECT, new Size(18, 18));
//        erode(diff_thresh, diff_thresh, kernel_erode);
////        Imgcodecs.imwrite(outPath+"/out/A"+index+"_diff_thresh1.png",diff_thresh);
////        //5.膨胀
////        dilate(diff_thresh, diff_thresh, kernel_dilate);
////        imshow("dilate", diff_thresh);
////        Imgcodecs.imwrite(outPath+"/out/A"+index+"_diff_thresh2.png",diff_thresh);
//
//        int size=CompressUtil.gZip(OpenCvUtil.MatToBytes(diff_thresh,".jpg")).length;
//        if(waveData.isWaveData((double) size)){
//            Imgcodecs.imwrite(outPath+"/out"+index+".png",frame);
//            Imgcodecs.imwrite(outPath+"/A/out"+index+"_diff_thresh.png",diff_thresh);
//        };
//        waveData.addData((double) size);
//
//
////        System.out.println("diff size::"+String.format("%4d",index)+"::" + CompressUtil.gZip(OpenCvUtil.MatToBytes(diff_thresh,".jpg")).length+"::"+diff_thresh.elemSize()+"::"+diff_thresh.elemSize1());
//
//        return diff_thresh;//返回result
//    }

    public void setOutPath(String outPath) {
        this.outPath=outPath;
        FileUtil.createDir(outPath+"/A");


//        FileUtil.createDir(outPath+"/A");
    }

}
